home *** CD-ROM | disk | FTP | other *** search
- /* $VER: 1.0a ARx_IactExamples by Robin Evans (8 Jul 1993,15 Oct 1993) */
-
- foo = address()
- address REXX
- call trace 'B'
-
- call addlib('rexxsupport.library',0,-30,0)
-
- /* My thanks to Tom Miller on GEnie for pointing out this elegant **
- ** way to redirect output and input to a new window */
-
- arg SubR .
-
- /* set up various formatting codes in compound variables that **
- ** can be exposed to subroutines by using just the stem */
- csi='9b'x;f.slant=csi'3m'; f.bold=csi'1m'; f.norm=csi'0m'
- f.black=csi'31m'; f.white=csi'32m'; f.blue=csi'33m'
- f.lf = '0a'x; f.cls = csi'0;0H'csi'J'
-
- options prompt f.white':::' f.norm
-
- /* Create a new standard-I/O console, so that we can use say **
- ** and pull instead of writeln() and readln() for output and **
- ** input. Note: the { system "rx.."} used in the AG link is **
- ** there because this setup won't work when AG is called by icon **
- ** unless that kludge is there. Using AG's RX command, the input **
- ** stream isn't opened under a workbench call */
- call close STDOUT
- if open(STDOUT, "con:98/8/584/345/ARexxGuide Examples/NOCLOSE", w) then do
- call close STDIN
- call open STDIN, "*", W
- call pragma '*' STDOUT
- call pragma '*' STDIN
- /* for some odd reason, the Amiga shell won't reliably call an **
- ** external function when at the default Amigaguide address */
- address REXX
- interpret 'call' SubR 'CLOSE'
- call close STDOUT
- call close STDIN
- call pragma('*')
- return 0
- address
- end
- else
- signal error
-
- SYNTAX:
- ErrCo = rc
- ERROR:
- signal off SYNTAX /* to prevent any possibility of an endless loop */
-
- say '0a'x
- say 'Sorry, an unexpected error has occured in line' SIGL
- if datatype(ErrCo, 'N') then
- say ' 'ErrCo':' errortext(ErrCo)
- call delay(1000)
- push endcli
- exit 9
-
-
- /* Get rid of a range of unprintable characters */
- arg inputfile
- call open($t,inputfile,'r')
- call open($n, 't:noNonPrint','W')
- do until eof($t)
- ln=readln($t)
- nl=compress(ln,xrange('0'x, '19'x))
- call writeln($n, nl)
- end
-
- exit
-
- /* $VER: 1.0 ShowNumeric.rexx by Robin Evans (21 May 1993) */
-
- ShowNumeric: procedure expose f.
-
- /* Demonstrate the effect of different settings of NUMERIC DIGITS
- ** and NUMERIC FUZZ
- */
- arg CLOSE
- options failat 21 /* to retain control when an error occurs */
- signal on ERROR
- signal on SYNTAX
-
- say f.white||'This example will demonstrate how different settings of'
- say 'NUMERIC DIGITS and NUMERIC FUZZ affect the comparison'
- say 'of numeric values.' '0a'x
- say 'Enter two numbers separated by at least one space, then'
- say 'press <Enter>.'
- say f.lf'To quit, press <Q> and <Enter>.'f.lf||f.norm
-
- do MainLoop = 1 /* exits on the datatype() check 4 lines down */
- say f.lf||f.blue'Enter two numbers to be compared:'
- pull Num.1 Num.2 .
- do i = 1 to 2
- /* any non-numeric value (including null) causes an exit from
- ** the main loop above.
- */
- Num.i = compress(Num.i, ',')
- if ~datatype(Num.i,N) then do
- if ~abbrev(upper(Num.i), 'Q') then do
- say 'You must enter two numbers.'f.white ' <Enter' f.blue'Q'f.white 'to quit>'f.black
- iterate MainLoop
- end
- else
- leave MainLoop
- end
- /* a decimal point is not considered part of a number's length */
- Num.i.len = length(compress(Num.i,'.'))
- end
- MNum = max(num.1, num.2)
- XLen = max(num.1.len, num.2.len)
- numeric digits min(XLen, 14)
- if XLen > 14 then do
- say f.white'The greatest precision available in ARexx is 14 digits.'
- say 'The number you entered with' XLen 'digits would always'
- say 'be rounded to the closest 14-digit value:'||f.norm
- /*
- ** the prefix + sign causes MNum to be evaluated according to the
- ** current digits() setting
- */
- say ' ' (+MNum) '0a'x
- XLen = 14
- end
- NLen = min(num.1.len, num.2.len)
- /* begin with a setting which will handle the largest number entered */
-
- /* if the numbers are equal under the most precise setting, then
- ** they will be equal under any other setting as well
- */
- if num.1 = num.2 then do
- say num.1 'will always be equal in any comparison to' num.2
- call ShowImprecise
- end
- else do
- numeric fuzz digits() - 1
- /*
- check for equality under the least precise setting and then
- find out the most precise setting at which the two are equal
- */
- if num.1 = num.2 then do
- numeric fuzz /* reset to make the first comparison at 0 */
- do i=0 to XLen-1 while num.1 ~= num.2
- numeric fuzz i
- end
- if num.1 = num.2 then do
- say num.1 'is considered equal to' num.2 'under these conditions:'
- say ' DIGITS setting of' digits()
- say ' FUZZ setting of' fuzz()
- say ' or at a FUZZ setting of 0 and DIGITS setting of' digits() - fuzz()
- numeric fuzz
- say f.white' The following table shows how the numbers are presented'
- say ' under different settings of NUMERIC DIGITS.'
- say ' Digits() 'left(Num.1,18) (Num.2)
- say f.blue' --------- ------------------ ------------------'f.norm
- do j = max(1,digits()-i) to xlen until strip(MNum) == (+MNum)
- numeric digits j
- say ' 'center(digits(),11) left((+Num.1),18) (+Num.2)
- end
- end
- end
- else do
- say max(num.1, num.2) 'will always be considered greater than' min(num.1, num.2)
- call ShowImprecise
- end
- end
- numeric fuzz
- numeric digits
- end
- if close = 'CLOSE' then
- push endcli
- return 0
-
- ShowImprecise:
-
- say f.white' The following chart shows the two numbers under the'
- say ' most imprecise settings of NUMERIC DIGITS 1 and 2'
- say ' 'left(Num.1,15) Num.2
- say f.blue' -------------- --------------'f.norm
- numeric fuzz
- numeric digits 1
- say ' 'left((+Num.1),15) (+Num.2)
- numeric digits 2
- say ' 'left((+Num.1),15) (+Num.2)||f.norm
- return
-
- /* $VER: 1.0 ShowSTDIO.rexx by Robin Evans (11 Jun 1993) */
-
- ShowSTDIO: procedure expose f.
-
- /* Demonstrate the effect of redirected IO. */
-
- arg CLOSE
- options failat 21 /* to retain control when an error occurs */
- signal on ERROR
- signal on SYNTAX
-
- LFS = f.lf||f.white
-
- do forever /* Loop allows reentering the demonstration */
- say f.cls
- say 'This demonstration will write a small ARexx file to the t:'
- say 'directory.'
- say
- say 'That file will be called with various forms of redirection'
- say 'to demonstrate the effect of redirection characters on '
- say 'ARexx files.'
-
- /* Save the demo file to the ram: disk T: directory */
- TFName = 't:testIO.rexx'
- TestCode = '/**/'f.lf'options prompt "0a"x||"Enter any text then press <Enter>: "'f.lf'pull T$'f.lf'say "0a"x||"You entered:" T$'f.lf
- if open(TFile, TFName, w) then do
- call writech(TFile, TestCode)
- call close TFile
- if QKey() then return 0
- say LFS'The file has been written to the T: directory:'
- call showSPrompt('list' left(TFName, 5)'#?')
- call showSPrompt( 'type' TFname)
- if QKey() then return 0
- say f.lf LFS'We will now run that program. Enter some text when prompted:'
- call showSPrompt('rx' TFname)
- say LFS'Notice that the program output to the shell the text you entered.'
- if QKey() then return 0
- say LFS'We''ll run it again, but this time we''ll redirect output using'
- say ' the DOS ">" redirection operator.'
- call showSPrompt('rx' TFname '>T:IOutput')
- say LFS'Notice that the program didn''t output anything this time, even'
- say ' though the SAY instruction is still in the program.'
- say ' What happened? Observe:'
- call showSPrompt('type T:IOutput')
- say LFS'Because of the redirection operator, the output of SAY went to a'
- say ' file instead of to the screen.'
- if QKey() then return 0
- say LFS'What happens when both input and output are redirected?'
- call showSPrompt('rx' TFname '<'TFName '>T:IOutput')
- say LFS'There was no prompt this time because the PULL instruction was'
- say ' redirected to look for its input from the file "'TFName'".'
- say ' It pulled the first line from that file:'
- call showSPrompt('type T:IOutput')
- end
- say f.lf'This concludes the demonstration.'
- say ' Press <Enter> to quit or <R> and <Enter> to repeat the demo.'
- pull Rsp
- if Rsp ~= 'R' then leave
- end
- return 0
-
- QKey: procedure expose f.
- options prompt f.lf||f.blue' Type <Q> and <Enter> to quit. Press <Enter> alone to continue.'f.norm
- pull QKey
- if QKEY = Q then return 1
- else return 0
-
- ShowSPrompt: procedure expose f.
- address command
- parse arg DCmd
- call writech(STDOUT, f.lf||f.blue'Shell'f.white'> 'f.norm)
- call delay(30)
- say DCmd
- ""DCmd
- return 0
-
- DoBreak: procedure expose f.
- /* Show how the break keys work in a subroutine */
- signal off break_e
- Say f.white" Press Control and E to stop the obnoxious listing that"
- say " will follow this message."f.black
- if QKey() then return
- NumRepeats = AdInfinit()
- say f.white||f.lf'The message was repeated' NumRepeats 'times.'
- say f.lf'We have returned from a subroutine to the main code of'
- say 'the program. The break key was detected within the subroutine'
- say 'but control could still be returned to the main program.'
- say f.lf||f.white' This demo is coded in the file' f.blue'ARx_IactExamples.rexx'
- say f.white' in the subroutine' f.blue'DoBreak:'
- say f.black||f.lf'- Press any key - '
- pull .
- return
-
- /* The subroutine being called by SIGNAL can be anywhere in **
- ** program. PROCEDURE¤¤, used in AdInfinit blinds it to **
- ** variables in the main program, but still allows the **
- ** BREAK_E subroutine to retrieve the [Rep] variable. */
-
- BREAK_E:
- say f.blue'Break detected at line' SIGL':'
- say f.white||sourceline(SIGL)
- return Rep
-
- AdInfinit: PROCEDURE expose f.
- /* turning on the signal within the subroutine¤¤ means **
- ** it will be effective only while this subroutine is **
- ** active */
- signal on break_e
- do Rep = 1
- say 'Press Ctrl-E at any time.'
- call delay 25
- say 'Stop me. Please.'
- end
- /* because the loop¤¤ above is endless, this RETURN¤¤ **
- ** will never be reached. */
- return 0
-
-